package com.hyperion.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyperion.blog.admin.mapper.PermissionMapper;
import com.hyperion.blog.admin.model.params.PageParam;
import com.hyperion.blog.admin.pojo.Permission;
import com.hyperion.blog.admin.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Hyperion
 * @date 2021/10/20
 */
@Service
public class PermissionService {

    @Resource
    public PermissionMapper permissionMapper;

    /**
     * 返回权限分页
     *
     * @param pageParam 分页
     * @return 分页表
     */
    public PageResult<Permission> listPermission(PageParam pageParam) {
        Page<Permission> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getQueryString())) {
            queryWrapper.eq(Permission::getName, pageParam.getQueryString());
        }
        Page<Permission> permissionPage = permissionMapper.selectPage(page, queryWrapper);
        PageResult<Permission> pageResult = new PageResult<>();
        pageResult.setList(permissionPage.getRecords());
        pageResult.setTotal(permissionPage.getTotal());
        return pageResult;
    }

    /**
     * 增加权限
     *
     * @param permission 权限
     */
    public void add(Permission permission) {
        this.permissionMapper.insert(permission);
    }

    /**
     * 更新权限
     *
     * @param permission 权限
     */
    public void update(Permission permission) {
        this.permissionMapper.updateById(permission);
    }

    /**
     * 删除权限
     *
     * @param id 权限id
     */
    public void delete(Long id) {
        this.permissionMapper.deleteById(id);
    }
}
